﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
namespace BeatEmUp
{
    class ResolutionManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
      
        int standardX = 1024;
        int standardY = 768;

        public ResolutionManager(Game game):
            base(game)
        {}

        internal void Draw(
            Microsoft.Xna.Framework.Graphics.Texture2D texture1, 
            Microsoft.Xna.Framework.Rectangle rect1)
        {

            if ((((Game)this.Game).graphics.PreferredBackBufferHeight == 768) &&
                (((Game)this.Game).graphics.PreferredBackBufferWidth == 1024))
                ((Game)this.Game).SpriteBatch.Draw(texture1, rect1, Color.White);
            else
            {
                int currentX = ((Game)this.Game).graphics.PreferredBackBufferWidth;
                int currentY = ((Game)this.Game).graphics.PreferredBackBufferHeight;
                double ratioX = (double)currentX/(double)standardX;
                double ratioY = (double)currentY/(double)standardY;
                
                rect1.Width = (int)(ratioX * rect1.Width);
                rect1.Height = (int)(ratioY * rect1.Height);
 
                ((Game)this.Game).SpriteBatch.Draw(texture1, 
                    new Rectangle(
                        (int)(rect1.X), 
                        (int)(rect1.Y), 
                        (int)(rect1.Width), 
                        (int)(rect1.Height)), 
                        Color.White
                    );
            }
        }

        internal void Draw(
            Microsoft.Xna.Framework.Graphics.Texture2D texture,
            Microsoft.Xna.Framework.Rectangle sourceRectangle,
            Microsoft.Xna.Framework.Rectangle rect1)
        {

            if ((((Game)this.Game).graphics.PreferredBackBufferHeight == 768) && 
                (((Game)this.Game).graphics.PreferredBackBufferWidth == 1024))
                ((Game)this.Game).SpriteBatch.Draw(texture, rect1, Color.White);
            else
            {
                int currentX = ((Game)this.Game).graphics.PreferredBackBufferWidth;
                int currentY = ((Game)this.Game).graphics.PreferredBackBufferHeight;

                double ratioX = (double)currentX / (double)standardX;
                double ratioY = (double)currentY / (double)standardY;

                rect1.Width = (int)(ratioX * rect1.Width);
                rect1.Height = (int)(ratioY * rect1.Height);

                ((Game)this.Game).SpriteBatch.Draw(texture,
                    new Rectangle(
                        (int)(rect1.X),
                        (int)(rect1.Y),
                        (int)(rect1.Width),
                        (int)(rect1.Height)), 
                    sourceRectangle,
                    Color.White
                    );
            }
        }

        internal void ChangeResolutionTo(int p, int p_2)
        {
            ((Game)this.Game).graphics.PreferredBackBufferWidth = p;
            ((Game)this.Game).graphics.PreferredBackBufferHeight = p_2;
            ((Game)this.Game).graphics.ApplyChanges();
        }

        internal void ChangeResolutionToDefault()
        {
            ((Game)this.Game).graphics.PreferredBackBufferWidth = standardX;
            ((Game)this.Game).graphics.PreferredBackBufferHeight = standardY;
            ((Game)this.Game).graphics.ApplyChanges(); 
        }

        internal void SwitchToFullScreen()
        {
           ((Game)this.Game).graphics.IsFullScreen = true;
           ((Game)this.Game).graphics.ApplyChanges();
        }

        internal void SwitchToWindowMode()
        {
            ((Game)this.Game).graphics.IsFullScreen = false;
            ((Game)this.Game).graphics.ApplyChanges();
        }
    }
}
